package com.dhcc.android.base.common.view;

/**
 * 
 */

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.utils.StringUtil;

import java.util.List;

/**
 * 带ListView的中间弹框
 * 
 * @author pengling
 * @date 2014-9-19
 */
public class ListViewPoupWindow {
	private MyPopupWindow popupwindow;
	private Activity activity;
	private View view;
	private ClickListenerInterface clickListenerInterface;

	public ListView listView;

	private String titleText;
	public ListViewPoupWindow(Activity activity,String titleText) {
		this.titleText=titleText;
		this.activity = activity;
	}

	public interface ClickListenerInterface {
		public void doClose();
	}

	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	public void initPopWindow(View parent, List<KeyValueBean> strList, boolean isDelete) {
		if (popupwindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) activity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.util_poup_listview, null);
			popupwindow = new MyPopupWindow(view, LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT,true);
			popupwindow.setAnimationStyle(R.style.PopupCenterAnimation);
			view.findViewById(R.id.close).setOnClickListener(itemsOnClickBtn);
			TextView title_text=(TextView) view.findViewById(R.id.title_text);
			if(StringUtil.isNotEmpty(titleText)){
				title_text.setText(titleText);
			}
			ListView listView = (ListView) view.findViewById(R.id.listview);
			listView.setDividerHeight(1);
			ListViewPoupAdapter listViewPoupAdapter = new ListViewPoupAdapter(
					activity, strList,isDelete);
			listView.setAdapter(listViewPoupAdapter);
			this.listView = listView;
		}
		ColorDrawable dw = new ColorDrawable(-00000);
		popupwindow.setBackgroundDrawable(dw);
		popupwindow.update();
		popupwindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER, 0,
				0);// 显示在最中央的位置

	}

	public void dismiss() {
		if (popupwindow != null) {
			popupwindow.dismiss();
		}
	}

	// 为弹出窗口实现监听类
	private OnClickListener itemsOnClickBtn = new OnClickListener() {
		public void onClick(View v) {
			if (popupwindow != null) {
				popupwindow.dismiss();
			}
			// switch (v.getId()) {
			// case R.id.close:
			// clickListenerInterface.doClose();
			// break;
			// default:
			// break;
			// }
		}
	};

}
